﻿namespace Tfs.OnTheRoad.Pages
{
    using System;
    using System.Windows;
    using GalaSoft.MvvmLight.Messaging;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Tfs.OnTheRoad.Commands;
    using Tfs.OnTheRoad.ViewModel;

    /// <summary>
    /// Base Page holding some Key Functionalities
    /// One of them is the Creation of the Application Bar, allowing some reusable Buttons(DefaultBarButtons enum)
    /// </summary>
    public class BasePage : PhoneApplicationPage
    {
        protected void CreateBar()
        {
            this.ApplicationBar = new ApplicationBar();
            this.ApplicationBar.IsVisible = false;
            this.ApplicationBar.IsMenuEnabled = false;            

            if (NavigationService!=null && NavigationService.CanGoBack && (DefaultBarButtons.Back & ApplicationBarButtons) == DefaultBarButtons.Back)
                CreateNewButton("Back", "/Images/appbar.back.rest.png",
                            delegate(object s, EventArgs e)
                            {
                                NavigationService.GoBack();
                            });

            if ((DefaultBarButtons.Settings & ApplicationBarButtons) == DefaultBarButtons.Settings)
                CreateNewButton("Settings", "/Images/appbar.feature.settings.rest.png",
                                        delegate(object s, EventArgs e)
                                        {
                                            NavigateToSettingsCommand toSettings = new NavigateToSettingsCommand();
                                            toSettings.Execute(null);
                                        });

            if ((DefaultBarButtons.Refresh & ApplicationBarButtons) == DefaultBarButtons.Refresh)
                CreateNewButton("Refresh", "/Images/appbar.refresh.rest.png",
                                        delegate(object s, EventArgs e)
                                        {
                                            //if it is bound to a viewModel we just call the Refresh from ViewModel
                                            if (this.DataContext is BaseViewModel)
                                                ((BaseViewModel)this.DataContext).Refresh();
                                            else
                                            //Otherwise we call the refresh method from the page
                                                Refresh();
                                        });

            if ((DefaultBarButtons.About & ApplicationBarButtons) == DefaultBarButtons.About)
                CreateNewButton("About", "/Images/appbar.questionmark.rest.png",
                            delegate(object s, EventArgs e)
                            {
                                NavigationService.Navigate(new Uri("/Pages/AboutPage.xaml", UriKind.Relative));
                            });
            this.ApplicationBar.IsVisible = true;
        }
        
        protected virtual void Refresh()        
        {
        }

        [Flags]
        public enum DefaultBarButtons
        {
            Refresh = 1,
            About= 2,
            Settings = 4,
            Back = 8
        }
        
        protected ApplicationBarIconButton CreateNewButton(string text, string imagePath, EventHandler click)
        {
            if(this.ApplicationBar==null)
                this.ApplicationBar = new ApplicationBar();
            var newButton = new ApplicationBarIconButton() { Text = text, IconUri = new Uri(imagePath, UriKind.Relative) };
            newButton.Click += click;
            this.ApplicationBar.Buttons.Add(newButton);
            return newButton;
        }
        public DefaultBarButtons ApplicationBarButtons { get; set; }
        public bool ShowSettingsButton { get; set; }
        public bool ShowAboutButton { get; set; }
        public bool ShowRefreshButton { get; set; }
        public BasePage()
            : base()
        {
            this.Loaded += delegate(object s, RoutedEventArgs e) { CreateBar(); };            
        }
        
        

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            Messenger.Default.Send<PhoneApplicationPage>(this, "RestoreState");
            base.OnNavigatedTo(e);            
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            Messenger.Default.Send<PhoneApplicationPage>(this, "SaveState");
            base.OnNavigatedFrom(e);            
        }
    }
}
